<?php
/**
 * TGLog
 * 功能：上报日志到TGLog
 *
 * @version 1.0.0 2019/04/01
 * @since
 *
 */
namespace Lib\Service;

use Lib\Base\Validate;

class TGLog
{
    private $domain;
    private $port;
    private $table;
    private $separator = "|";
    private $terminator = "\n";

    /**
     * 构造函数
     * @param string $domain tglog的域名
     * @param int $port tglog的端口
     * @param string $table 数据上报的表名
     *
     */
    public function __construct($option)
    {
        if (!empty($option['domain'])) {
            $this->domain = $option['domain'];
        }
        if (!empty($option['port'])) {
            $this->port = $option['port'];
        }
        if (!empty($option['table'])) {
            $this->table = $option['table'];
        }
    }

    /**
     * 设置要上报的表名
     * @param string $table 上报数据的表名
     */
    public function setTbName($table)
    {
        if (!empty($table)) {
            $this->table = $table;
        }
    }

    /*
     * 上报日志到TGLog
     * @param array 数字下标组成的一维数组，数组每个元素为要上报的字段的值，下标从小到大依次匹配对应的字段
     * @return bool true-成功，false-失败
     */
    public function sendLog($data)
    {
        recordLog('tglog report data:' . var_export($data, true), __FILE__, __LINE__);

        if (empty($this->domain) || empty($this->port) || empty($this->table)) {
            recordLog('tlog服务器信息错误，domain:' . $this->domain . ', port:' . $this->port . ', table:' . $this->table, __FILE__, __LINE__, LP_ERROR);
            return false;
        }

        if (empty($data) || !is_array($data)) {
            recordLog('要上报的数据不能为空，且必须是数字下标的一维数组，value为要上报的数据！', __FILE__, __LINE__, LP_ERROR);
            return false;
        }

        foreach ($data as $index => $value) {
            if (!Validate::checkNaturalNum($index, 1)) {
                recordLog('要上报的数据不能为空，且必须是数字下标的一维数组，value为要上报的数据！', __FILE__, __LINE__, LP_ERROR);
                return false;
            }
            if (!is_string($value) && !is_numeric($value)) {
                recordLog('要上报的数据不能为空，且必须是数字下标的一维数组，value为要上报的数据！', __FILE__, __LINE__, LP_ERROR);
                return false;
            }
        }

        $ip = gethostbyname($this->domain);

        recordLog('tglog report ip:' . $ip, __FILE__, __LINE__);

        $ipint = ip2long($ip);
        if ($ipint === false) {
            recordLog('通过域名解析ip时出现错误，domain:' . $this->domain, __FILE__, __LINE__, LP_ERROR);
            return false;
        }

        $sendData = $this->table . $this->separator . implode($this->separator, $data) . $this->terminator;

        recordLog('tglog report senddata:' . var_export($sendData, true), __FILE__, __LINE__);

        $udp = new \UdpDgram();
        $conRet = $udp->connect($ip, $this->port);
        if ($conRet != 0) {
            recordLog('数据上报tglog时udp连接失败，conRet:' . var_export($conRet, true), __FILE__, __LINE__, LP_ERROR);
            return false;
        }
        $ret = $udp->send($sendData);
        if ($ret <= 0) {
            recordLog('数据上报tglog失败，ret:' . var_export($ret, true), __FILE__, __LINE__, LP_ERROR);
        }
        return $ret > 0 ? true : false;
    }
}
